<?php
namespace app\api\controller;

use think\Db;
use think\Request;
use think\Controller;

class Common extends Controller
{

    public function _initialize()
    {
        $post = Request::instance()->post();
       // file_put_contents('sign.txt',json_encode($post));
        if (isset($post['timestamp']) && $post['timestamp'] != '' && isset($post['nonce_str']) && $post['nonce_str'] != '' && isset($post['sign']) && $post['sign'] != '') {
            //检查请求是否过期
            if (time()-$post['timestamp']>600){
                exit(json_encode(['code'=>501,'msg'=>'请求已过期']));
            }

            //检查随即字符串
            $nonce_str = Db::name('nonce_str')->where("nonce",$post['nonce_str'])->find();
            if ($nonce_str){
                exit(json_encode(['code'=>502,'msg'=>'重复请求']));
            }

            //检查签名是否正确
            $sign = self::MakeSign($post);
          //  file_put_contents('sign1.txt',$sign);
            if ($sign !== $post['sign']){
                exit(json_encode(['code'=>503,'msg'=>'签名验证失败']));
            }

            //将nonce_str写入数据库
            //删除十分钟之前的nonce
            //将新的nonce写入数据库
            $now = time();
            Db::name('nonce_str')->where("{$now}-time>600")->delete();
            Db::name('nonce_str')->insert(array('time'=>time(),'nonce'=>$post['nonce_str']));

            //检查token
            if (!$post['token']||!$post['uid']){
                exit(json_encode(['code'=>500,'msg'=>'请重新登录']));
            }

            $checkUserToken['uid']    = $post['uid'];
            $checkUserToken['token'] = $post['token'];

            $checkUserTokenResult = Db::name('user')->where($checkUserToken)->find();
            $is_del = Db::name('user')->where('uid',$post['uid'])->find();

            if($is_del['is_del']==1){
                exit(json_encode(['code'=>500,'msg'=>'您的账号已被禁用,如有疑问请联系客服']));
            }
            if (!$checkUserTokenResult) {
                exit(json_encode(['code'=>500,'msg'=>'请重新登录']));
            }

        }else{
            exit(json_encode(['code'=>504,'msg'=>'请求已过期']));
        }
    }



    /**
     * 生成签名
     */
    private function MakeSign($arrToSign)
    {
        //签名步骤一：按字典序排序参数
        ksort($arrToSign);
        $string = "";
        foreach ($arrToSign as $k => $v)
        {
            if($k != "sign" && $v != "" && !is_array($v)){
                $string .= $k . "=" . $v . "&";
            }
        }
        $string = trim($string,"&");
        //签名步骤二：在string后加入KEY
        $string = $string . config('SecretKey');
        file_put_contents('1.txt',$string);
        //签名步骤三：MD5加密
        $string = sha1(md5($string));
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    /**
     * 接口返回封装
     */
    protected function apiReturn($code='-1',$msg,$data=[]){
        $code = (string)$code;
        if($code == '-1'){
            exit('参数调用错误');
        }
        if(empty($data)){
            if($code=='200'){
                exit(json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data)));
            }else{
                exit(json_encode(array('code'=>$code,'msg'=>$msg)));
            }
        }else{
            exit(json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data)));
        }
    }
    /*
     * 接口分页类
     */
    protected function p()
    {
        //客户端传过来的分页数字
        $p = input('page',1,'intval');
        if($p < 1){
            $p = 1;
        }
        return $p;
    }
    //处理时间
    function time_ago($posttime){
        //当前时间的时间戳
        $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
        //之前时间参数的时间戳
        $posttimes = strtotime($posttime);

        //相差时间戳
        $counttime = $nowtimes - $posttimes;

        //进行时间转换
        if($counttime<=60){

            return '刚刚';

        }else if($counttime>60 && $counttime<3600){

            return intval(($counttime/60)).'分钟前';

        }else if($counttime>=3600 && $counttime<3600*24){

            return intval(($counttime/3600)).'小时前';

        }else if($counttime>=3600*24 && $counttime<3600*24*2){

            return '昨天';

        }else if($counttime>=3600*24*2 && $counttime<3600*24*3){

            return '前天';

        }else if($counttime>=3600*24*3 && $counttime<=3600*24*30){

            return intval(($counttime/(3600*24))).'天前';

        }else{

            return $posttime;

        }
    }
}